home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / getrom.c < prev    next >
C/C++ Source or Header  |  1997-07-30  |  3KB  |  126 lines

  1. /* getrom:  Fetch ROM image for CoPilot
  2.  *
  3.  * This is free software, licensed under the GNU Public License V2.
  4.  * See the file COPYING for details.
  5.  *
  6.  * Written by Kenneth Albanowski, based on code by Greg Hewgill
  7.  *
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include "pi-source.h"
  13. #include "pi-serial.h"
  14.  
  15. #include <termios.h>
  16.  
  17. char * progname;
  18.  
  19. void Help(void)
  20. {
  21.   fprintf(stderr,"usage: %s [-2] %s\n",progname,TTYPrompt);
  22.   exit(2);
  23. }
  24.  
  25. struct record * records = 0;
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29.   int l,p;
  30.   char buf[0xffff];
  31.   int i;
  32.   struct pi_sockaddr addr;
  33.   unsigned char check;
  34.   struct pi_socket ps;
  35.   extern char* optarg;
  36.   extern int optind;
  37.   int rom;
  38.   int version = 1;
  39.   int max;
  40.  
  41.   progname = argv[0];
  42.  
  43.   if (argc < 2)
  44.     Help();
  45.   
  46.   if (strcmp(argv[1], "-2")==0) {
  47.     version = 2;
  48.     argv++;
  49.     argc--;
  50.   }
  51.   
  52.   addr.pi_family = PI_AF_SLP;
  53.   strcpy(addr.pi_device, argv[1]);
  54.   
  55.   ps.mac = calloc(1, sizeof(struct pi_mac));
  56.   ps.rate = 38400;
  57.   ps.sd = 0;
  58.   if (pi_serial_open(&ps, &addr, sizeof(addr)) == -1) {
  59.     perror("Unable to open port");
  60.     exit(0);
  61.   }
  62.  
  63.   rom = open((version == 2) ? "pilot2.rom" : "pilot.rom",O_WRONLY|O_CREAT|O_TRUNC,0666);
  64.   if (rom == -1) {
  65.     perror("Unable to create pilot.rom");
  66.     exit(0);
  67.   }
  68.   
  69.   printf("\
  70. NOTICE: Use of this program may place you in violation of your license\n\
  71.         agreement with US Robotics. Please read page 123 of your Pilot\n\
  72.         handbook (\"Software License Agreement\") before running this program.\
  73. \n\n");
  74.                       
  75.   printf("Please start %s on your Pilot.\n", (version == 2) ? "Getrom2.prc" : "Getrom.prc");
  76.   printf("Waiting for connection on %s...\n", argv[1]);
  77.  
  78.   max = (version == 2) ? 256 : 128;
  79.   for(i=0;i < max;i++) {
  80.         do {
  81.           l = read(ps.mac->fd, buf, 1);
  82.           if (l<1)
  83.             continue;
  84.       } while (buf[0] != '*');
  85.       printf("\r%d/%d", i+1, max);
  86.         fflush(stdout);
  87.       
  88.       p = 0;
  89.       do {
  90.         l = read(ps.mac->fd, buf+p, 4096-p);
  91.         if (l<0) {
  92.           perror("Unable to read sync byte");
  93.         }
  94.         p += l;
  95.       } while( p<4096);
  96.       
  97.       check = 0xff;
  98.       if (read(ps.mac->fd, buf+4096, 1)<0) {
  99.         perror("Unable to read checksum byte");
  100.         goto error;
  101.       }
  102.       
  103.       for (p=0;p<4096;p++)
  104.         check = ((check << 1) | check >> 7) ^ buf[p];
  105.       
  106.       if ((unsigned char)buf[4096] != check) {
  107.           printf("\nChecksum error\n");
  108.           goto error;
  109.       }
  110.       
  111.       write(rom, buf, 4096);
  112.       
  113.       buf[4096] = '+';
  114.       
  115.       write(ps.mac->fd, buf+4096, 1);
  116.   }
  117.   printf("\nSuccessful!\n");
  118.  
  119. error:
  120.   close(rom);
  121.           
  122.   ps.serial_close(&ps);
  123.   
  124.   exit(0);
  125. }
  126.